<%@page import="net.groupme.group.GroupListsManager"%>
<%@page import="java.util.List"%>
<%@page import="net.groupme.model.Group"%>
<%@page import="java.util.Iterator"%>
<%@page import="net.groupme.model.MODEL_CONSTANTS"%>
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.*"%>
<%@page import="net.groupme.model.Tag"%>
<div class="grouplistHead" style="margin-top:1em;"><h4>Latest Groups</h4></div>
<% GroupListsManager glm = new GroupListsManager(); 
	List latest = glm.get10LatestGroups();
	
	int currentPage = 0;
	if(request.getParameter("pageID") != null){
		currentPage = Integer.parseInt(request.getParameter("pageID"));
	}else {
		currentPage = 1;
	}
	
	//start and end of the list-iterator
	int iterStart = (currentPage * 10) - 10;
	int iterEnd = iterStart + 9;
	
	int numberofGroups = latest.size();

%>
	<div id="mainGroupwindowContent" style="margin-top: 0.5em;">
		
	<% 
	boolean editGroups = false;
	ListIterator<Group> it = latest.listIterator(iterStart); %>
	<%@ include file="./groupme_list_groups.jsp" %>
	</div>
	<div id="pagenumbers" style="text-align: center; margin-top: 1em; font-size:0.8em;">
	<%
		int resultListSize = numberofGroups;
		if(resultListSize > 10){
			int numberofResultpages = (int) Math.ceil(resultListSize / 10.0);
			String pagerequestURL = request.getContextPath() + "/user/";

			if(currentPage > 1){
				int showPrev = currentPage-1;
				%>
				<span><strong><a href="<%= pagerequestURL + showPrev %>">previous</a></strong></span>&nbsp;&nbsp;
				<%
			}
			out.print("<span>");
			if(numberofResultpages <= 10){
				for(int i = 1; i <= numberofResultpages; i++){
					%>
					<a href="<%= pagerequestURL + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;\"");}%>><%=i %></a>
					<%
				}
			}else {
				if(numberofResultpages - currentPage >= 10){
					int start = 0;
					int end = 0;
					
					if(currentPage < 7){
						start = 1;
						end = 7;
					}else {
						
						%>
						<a href="<%= pagerequestURL + 1 %>"><%=1 %></a>
						<%
						out.print("&nbsp;...&nbsp;");
						start = currentPage - 3;
						end = currentPage + 3;
					}
					
					for(int i = start; i <= end; i++){
						%>
						<a href="<%= pagerequestURL + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;\"");}%>><%=i %></a>
						<%
					}
					out.print("&nbsp;...&nbsp;");
					%>
					<a href="<%= pagerequestURL + numberofResultpages %>"><%=numberofResultpages %></a>
					<%
				}
				else {
					%>
					<a href="<%= pagerequestURL + 1 %>"><%=1 %></a>
					<%
					out.print("&nbsp;...&nbsp;");
					//out.print("...&nbsp;");
					for(int i = numberofResultpages - 10; i <= numberofResultpages; i++){
						%>
						<a href="<%= pagerequestURL + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;\"");}%>><%=i %></a>
						<%
					}
				}
			}
			out.print("</span>");
			if(currentPage < numberofResultpages){
				int showNext = currentPage+1;
				%>
				<span><strong><a href="<%= pagerequestURL + showNext %>">next</a></strong></span>&nbsp;&nbsp;
				<%
			}
		}
	%>
</div>